package com.example.nacosprovider.service.impl;

import  com.example.nacosprovider.dao.JavaUserMapper;
import com.example.nacosprovider.pojo.*;
import com.example.nacosprovider.service.JavaUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.nacosprovider.dao.JavaUserMapper;
import com.example.nacosprovider.pojo.JavaUser;
import com.example.nacosprovider.service.JavaUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
* @author DELL
* @description 针对表【java_user】的数据库操作Service实现
* @createDate 2022-08-09 21:43:44
*/
@Service
public class JavaUserServiceImpl extends ServiceImpl<JavaUserMapper, JavaUser>
implements JavaUserService {
    @Resource
    private JavaUserMapper javaUserMapper;
    @Resource
    SessionRegistry sessionRegistry;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 用户注册
     * @param javaUser 接收一个注册信息
     */
    @Override
    public void insertData(JavaUser javaUser) {
        System.out.println("ok");
        javaUserMapper.insert(javaUser);
        System.out.println("插入成功");
    }

    /**
     * 当用户登陆时修改用户的最近登录时间信息
     * @param userName 接收用户名
     */
    @Override
    public void updateLoginTime(String userName) {
        UpdateWrapper<JavaUser> updateWrapper = new UpdateWrapper<>();
        Date date = new Date();
        updateWrapper.eq("user", userName).set("loginTime", date);
        javaUserMapper.update(null, updateWrapper);
    }

    /**
     * 返回当前为登录状态的用户
     * @param userList 当前登录的用户名列表
     * @return
     */
    public List<JavaUser> mannageUser(List userList) {
        //从会话注册表中获取所有已登录的用户
        List<Object> list = sessionRegistry.getAllPrincipals();
            QueryWrapper<JavaUser> wrapper = new QueryWrapper<>();
            wrapper.in(!userList.isEmpty(), "user", userList).select("user", "registTime", "loginTime");
            return javaUserMapper.selectList(wrapper);
        }

    /**
     * 修改用户的头像存储地址
     * @param userName
     * @param headsHotPath
     */
    @Override
    public void upLoadHeadsHot(String userName,String headsHotPath) {
        UpdateWrapper<JavaUser> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user", userName).set("headshotPath", headsHotPath);
        javaUserMapper.update(null, updateWrapper);
    }
}
